package com.cirkana.viewer.profile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;

import com.cirkana.serial.SerializablePassData;

public class Profile extends SerializablePassData {

	public static final String CREDENTIALS_TWITTER = "twitter";

	private ProfileCredential cirkanaCredential;
	private HashMap<String, ProfileCredential> credentials = new HashMap<String, ProfileCredential>();

	public Profile() {
	}
	
	public Profile(String user, String password) {
		this.cirkanaCredential = new ProfileCredential();
		this.cirkanaCredential.setUser(user);
		this.cirkanaCredential.setPassword(password);
		
		ProfileCredential credentialTwitter = new ProfileCredential();
		credentialTwitter.setUser("user");
		credentialTwitter.setPassword("password");
		this.credentials.put(CREDENTIALS_TWITTER, credentialTwitter);
	}

	public ProfileCredential getCirkanaCredential() {
		return cirkanaCredential;
	}

	public void setCirkanaCredential(ProfileCredential cirkanaCredential) {
		this.cirkanaCredential = cirkanaCredential;
	}

	@Override
	public void serialize(DataOutputStream out) throws IOException {
		if (this.cirkanaCredential == null)
			serializeBoolean(true, out);
		else {
			serializeBoolean(false, out);
			this.cirkanaCredential.serialize(out);
		}
		
		serializeInteger(this.credentials.size(), out);
		for (String key : this.credentials.keySet()) {
			serializeString(key, out);
			this.credentials.get(key).serialize(out);
		}
	}

	@Override
	public void deserialize(DataInputStream in) throws IOException {
		this.cirkanaCredential = null;
		if (!deserializeBoolean(in)) {
			this.cirkanaCredential = new ProfileCredential();
			this.cirkanaCredential.deserialize(in);
		}
		
		this.credentials.clear();
		int size = deserializeInteger(in);
		for (int iii = 0; iii < size; iii++) {
			String key = deserializeString(in);
			ProfileCredential profileCredential = new ProfileCredential();
			profileCredential.deserialize(in);
			this.credentials.put(key, profileCredential);
		}
	}

}
